package cn.gzjp.modules.it.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.service.CrudService;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.modules.it.entity.Car;
import cn.gzjp.modules.it.entity.CarMove;
import cn.gzjp.modules.it.entity.Member;
import cn.gzjp.modules.it.dao.CarMoveDao;

/**
 * 一键挪车Service
 * @author zengcp
 * @version 2018-02-02
 */
@Service
@Transactional(readOnly = true)
public class CarMoveService extends CrudService<CarMoveDao, CarMove> {
	public static Logger log = LoggerFactory.getLogger("womsLog");
	
	@Autowired
	private MemberService memberService;
	@Autowired
	private CarService carService;

	public CarMove get(String id) {
		return super.get(id);
	}
	
	public List<CarMove> findList(CarMove carMove) {
		return super.findList(carMove);
	}
	
	public Page<CarMove> findPage(Page<CarMove> page, CarMove carMove) {
		return super.findPage(page, carMove);
	}
	
	@Transactional(readOnly = false)
	public void save(CarMove carMove) {
		Member member = memberService.get(carMove.getMemberId());
		if(member==null){
			member = new Member();
			member.setId(carMove.getMemberId());
		}
		Car car = carService.get(carMove.getChepai());
		log.info("挪车申请对应车辆是否已绑定"+car);
		if(car!=null){
			String mobile = car.getMobile();
			log.info("挪车申请车牌对应手机号"+mobile);
			if(StringUtils.isNotBlank(mobile)&&mobile.length()==11){
				String chepai = carMove.getChepai().substring(carMove.getChepai().length()-2,carMove.getChepai().length());
				String content = "【商丘智慧交管】您尾号为"+chepai+"的车辆在【"+carMove.getAddress()+"】违停，请在10分钟内移走，逾期按违停处理。商丘交管文明车主提示：回复数字10可享十分钟免罚特权";
				log.info("挪车申请下发短信通知 mobile="+mobile+"|content="+content);
				SmsClientSend.sendSms(mobile, content);
			}
		}
		if(StringUtils.isNotBlank(carMove.getMobile())&&carMove.getMobile().length()==11){
			member.setMobile(carMove.getMobile());
			memberService.save(member);
		}
		super.save(carMove);
	}
	
	@Transactional(readOnly = false)
	public void delete(CarMove carMove) {
		super.deleteByLogic(carMove);
	}
	
}